package com.eshin.controller;

import java.util.Date;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.context.request.async.DeferredResult;

import com.eshin.bean.OrderInfo;
import com.eshin.service.OrderService;
import com.eshin.service2.OrderService2;

@Controller
public class OrderController {
    
    private static Logger LOGGER = LoggerFactory.getLogger(OrderController.class);
    
    @Autowired
    OrderService service;
    @Autowired
    OrderService2 service2;
    
    @Autowired
    RestTemplate restTemplate;
    
    
    @RequestMapping(path = "/get", method = RequestMethod.GET)
    public @ResponseBody
    OrderInfo orderGet(OrderInfo info) {
        info.setReqTransID("13925219347:"+System.currentTimeMillis());
        LOGGER.info("{} {}",info.getReqTransID(),"orderJson");
        LOGGER.warn("{} {}",info.getReqTransID(),"orderJson");
        LOGGER.error("{} {}",info.getReqTransID(),"orderJson");
        service.doService("get");
        service2.doService("get2");
        return info;
    }
    @RequestMapping(path = "/order", method = RequestMethod.GET)
    public @ResponseBody
    String order(@RequestParam("request_params") String requestParams) {
    	LOGGER.info("{} {}",requestParams,"orderJson");
    	LOGGER.warn("{} {}",requestParams,"orderJson");
    	LOGGER.error("{} {}",requestParams,"orderJson");
    	service.doService("get");
    	service2.doService("get2");
    	return "test";
    }
    @RequestMapping(path = "/getfuture", method = RequestMethod.GET)
    public @ResponseBody DeferredResult<String> orderFuture(OrderInfo info) {
    	DeferredResult<String> deferredResult = new DeferredResult<String>(3000L);
    	Future<String> future = getFuture();
//    	Future<String> future = getFuture1();
    	
    	new Thread(new Runnable() {
			
			@Override
			public void run() {
				try {
					Thread.sleep(1000L);
					System.out.println("new Thread get...");
					deferredResult.setResult(future.get());
				} catch (Exception e) {
				}
			}
		}).start();
    	
    	System.out.println("main thread...");
    	return deferredResult;
    }
	private Future<String> getFuture1() {
		Future<String> future = restTemplate.postForObject("https://www.baidu.com", "hello", Future.class);
		return future;
	}
	private Future<String> getFuture() {
		Future<String>future = new Future<String>() {
			
			@Override
			public boolean isDone() {
				// TODO Auto-generated method stub
				return false;
			}
			
			@Override
			public boolean isCancelled() {
				// TODO Auto-generated method stub
				return false;
			}
			
			@Override
			public String get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
//				Thread.sleep(3000L);
				System.out.println("future get...");
				return "future";
			}
			
			@Override
			public String get() throws InterruptedException, ExecutionException {
				Thread.sleep(500L);
				System.out.println("future get...");
				return "future";
			}
			
			@Override
			public boolean cancel(boolean mayInterruptIfRunning) {
				// TODO Auto-generated method stub
				return false;
			}
		};
		return future;
	}
    
    
}
